package com.workerhelping.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workerhelping.dto.RecordIdDTO;
import com.workerhelping.elasticsearchEntity.ESRecordVO;
import com.workerhelping.po.Confirm;
import com.workerhelping.po.Record;
import com.workerhelping.po.StuckReason;
import com.workerhelping.result.Result;
import com.workerhelping.service.AdminRecordService;
import com.workerhelping.service.AdminRecordVOService;
import com.workerhelping.service.ConfirmService;
import com.workerhelping.service.StuckReasonService;
import com.workerhelping.vo.AdminRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("adminRecord")
public class AdminRecordController {

    @Autowired
    private AdminRecordService adminRecordService;

    @Autowired
    private StuckReasonService stuckReasonService;

    @Autowired
    private ConfirmService confirmService;

    @Autowired
    private AdminRecordVOService adminRecordVOService;


    @GetMapping("/getAllByUnreviewed")
    public Result<Object> getAllByUnreviewed(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        try {
            IPage<AdminRecordVO> records = adminRecordVOService.getAllByUnreviewed(pageNo, pageSize);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("/getAllByApproved")
    public Result<Object> getAllByApproved(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        try {
            IPage<AdminRecordVO> records = adminRecordVOService.getAllByApproved(pageNo, pageSize);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("/getAllByRejected")
    public Result<Object> getAllByRejected(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        try {
            IPage<AdminRecordVO> records = adminRecordVOService.getAllByRejected(pageNo, pageSize);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("/getAllByCancelled")
    public Result<Object> getAllByCancelled(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        try {
            IPage<AdminRecordVO> records = adminRecordVOService.getAllByCancelled(pageNo, pageSize);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


    /**
     * 根据档案id获取档案
     *
     * @param recordIdDTO
     * @return
     */
    @PostMapping("/getRecordByRecordId")
    public Result<Record> getRecordByRecordId(@RequestBody RecordIdDTO recordIdDTO) {
        try {
            Record record = adminRecordService.getById(recordIdDTO.getId());
            return Result.ok(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


    /**
     * 根据档案id查询致困原因
     *
     * @param recordIdDTO
     * @return
     */
    @PostMapping("/getAllStuckReasonByRecordId")
    public Result<List<StuckReason>> getAllStuckReasonByRecordId(@RequestBody RecordIdDTO recordIdDTO) {
        try {
            List<StuckReason> stuckReasons = stuckReasonService.getAllStuckReasonByRecordId(recordIdDTO.getId());
            return Result.ok(stuckReasons);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 保存消息信息
     *
     * @param confirm
     * @return
     */
    @PostMapping("/saveConfirm")
    public Result<String> saveConfirm(@RequestBody Confirm confirm) {
        try {
            boolean isSave = confirmService.save(confirm);
            if (isSave)
                return Result.ok();
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

}
